package com.swinw.asumall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.swinw.asumall.product.entity.AttrAttrgroupRelationEntity;
import com.swinw.asumall.product.entity.AttrEntity;
import com.swinw.asumall.product.service.AttrAttrgroupRelationService;
import com.swinw.asumall.product.service.AttrService;
import com.swinw.asumall.product.service.CategoryService;
import com.swinw.asumall.product.vo.AttrGroupRelationVo;
import com.swinw.asumall.product.vo.AttrGroupWithAttrsVo;
import com.swinw.common.vaild.AddGroup;
import com.swinw.common.vaild.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.swinw.asumall.product.entity.AttrGroupEntity;
import com.swinw.asumall.product.service.AttrGroupService;
import com.swinw.common.utils.PageUtils;
import com.swinw.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author 罗义华
 * @email 2407550328@qq.com
 * @date 2020-10-04 12:33:33
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Autowired
    private AttrService attrService;

    @Resource
    private CategoryService categoryService;

    ///product/attrgroup/{catelogId}/withattr

    @GetMapping("/{catelogId}/withattr")
    public R attrGroupWithattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> data = attrGroupService.attrGroupWithattr(catelogId);
        return R.ok().put("data",data);
    }

    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 添加关联关系
     * @param voList
     * @return
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrGroupRelationVo> voList){
        relationService.saveBatch(voList);
        return R.ok();
    }
    /**
     * 分页查询属性分组信息
     * @param params 分页信息
     * @return
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 通过三级分类ID查询属性组信息
     * @param params 分页信息
     * @param catId 三级分类ID
     * @return
     */
    @RequestMapping("/list/{catId}")
    public R listByCatId(@RequestParam Map<String, Object> params,@PathVariable("catId")Long catId){
        PageUtils page = attrGroupService.queryPage(params,catId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPaths = categoryService.findCatalogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPaths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@Validated({AddGroup.class}) @RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    @RequestMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
//		attrService.removeByIds(Arrays.asList(attrIds));
        attrService.relationDelete(Arrays.asList(attrGroupRelationVo));
        return R.ok();
    }


    /**
     * 查询分组没有噶关联的属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.getNoRelationAttr(attrgroupId,params);
        return R.ok().put("page",pageUtils);
    }
}
